package com.xxb.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.xxb.entity.User;
import com.xxb.util.DBHelp;
import com.xxb.util.Rowmapper;

public class UserDao {
	
	private DBHelp<User> db = new DBHelp<User>();
	
	public int save(User user){
		String sql = "insert into t_user(useremail,userpwd,createtime) VALUES (?,?,?)";
		return db.executeSQL(sql, user.getUseremail(),user.getUserpwd(),user.getCreatetime());
	}
	
	public User login(String useremail,String userpwd){
		String sql ="select id,useremail,userpwd,createtime from t_user where useremail=? and userpwd=?";
		return db.queryObject(sql, new UserRowmapper(),useremail,userpwd);
	}
	
	private class UserRowmapper implements Rowmapper<User>{

		public User mapRow(ResultSet rs) throws SQLException {
			User user  = new User();
			
			user.setCreatetime(rs.getString("createtime"));
			user.setId(rs.getInt("id"));
			user.setUseremail(rs.getString("useremail"));
			user.setUserpwd(rs.getString("userpwd"));
			
			return user;
		}
		
	}
}
